define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'subject/subject/index' + location.search,
                    add_url: 'subject/subject/add',
                    edit_url: 'subject/subject/edit',
                    del_url: 'subject/subject/del',
                    info_url: 'subject/info/index',
                    chapter_url: 'subject/chapter/index',
                    table: 'subject',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'price', title: __('Price'), operate: 'BETWEEN' },
                        { field: 'category.name', title: __('Cateid') },
                        { field: 'thumbs_text', title: __('Thumbs'), operate: false, formatter: Table.api.formatter.image },
                        { field: 'teacher.name', title: __('Teacherid') },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [{
                                name: 'info',
                                title: function (data) {
                                    return `${data.title}-课程详情`
                                },
                                icon: 'fa fa-eye',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                url: $.fn.bootstrapTable.defaults.extend.info_url,
                                extend: 'data-toggle=\'tooltip\' data-area= \'["80%","100%"]\'',
                            },
                            {
                                name: 'chapter',
                                title: function (data) {
                                    return `${data.title}-章节详情`
                                },
                                icon: 'fa fa-align-justify',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                url: $.fn.bootstrapTable.defaults.extend.chapter_url,
                                extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "100%"]\'',
                            }
                            ]
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        del: function () {
            // 给控制器绑定通用事件
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
